/*
 * DescargarArxiu.java
 *
 */

package org.wooxes.server;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;

/**
 *
 * @author woox
 */
public class DescargarArxiu extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String ruta = "";
        String nom_adjunt = "";
        try{
            int idpractica = Integer.parseInt(request.getParameter("id"));            
            
            ConnexioSQL connexio = new ConnexioSQL();
            Connection conn = connexio.getConnection();
            ConfiguracioServidor config = new ConfiguracioServidor(conn);
            
            ruta = config.getUploadDir();
            
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("SELECT adjunt FROM activitat where id="+idpractica);
            
            //Si la pràctica existeix
            String st = null;
            while(rs.next()){
                st = rs.getString("adjunt");
            }

            if(st == null){
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();
                out.println("Error: No s'ha trobat cap adjunt");
                return;
            }
            else{
                nom_adjunt = st;
                ruta = ruta + idpractica + File.separator + nom_adjunt;
            }
        
        }
        catch(Exception e){
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.println("Error");
            return;
        }
        
       // Get the absolute path of the image
        ServletContext sc = getServletContext();
        String filename = ruta;
    
        // Get the MIME type of the image
        String mimeType = sc.getMimeType(filename);
        if (mimeType == null) {
            sc.log("Could not get MIME type of "+filename);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
    
        // Set content type
        response.setContentType(mimeType);
    
        // Set content size
        File file = new File(filename);
        response.setContentLength((int)file.length());
        response.setHeader("Content-Disposition","attachment; filename=\""+ nom_adjunt + "\"");
    
        // Open the file and output streams
        FileInputStream in = new FileInputStream(file);
        OutputStream out = response.getOutputStream();
    
        // Copy the contents of the file to the output stream
        byte[] buf = new byte[1024];
        int count = 0;
        while ((count = in.read(buf)) >= 0) {
            out.write(buf, 0, count);
        }
        in.close();
        out.close();
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
